package com.educate.book.web.controller;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.educate.book.model.bo.BookCatalogueBO;
import com.educate.book.model.bo.ItemBO;
import com.educate.book.rpc.api.ItemService;
import com.educate.common.web.R;

import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("item")
public class ItemController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}")
    private ItemService itemService;

    /**
     *  请求参数：{"desc":"0-3岁","name":"0-3岁","parentId","XXXXXXXX"}
     *  desc:描述
     *  name:名称
     *  parentId：父ID
     *  
 		返回结果:{"code":"000000","msg":"success","data":"{\"id\":\"a19a467237b2470e98a5ccb4286c3ca1\"}"}
 		id:为新增成功的ID
     * @param bo
     * @return
     */
    @PostMapping(value = "/create")
    public R create(@RequestBody ItemBO bo) {
    	String Id = itemService.create(bo);
    	JSONObject json = new JSONObject();
    	json.put("id", Id);
        return R.ok(json.toJSONString());
    }

    /**
     * 请求参数：{"itemId":"aff8389ff1534522884780c3ab1f02b7"}
 	        返回结果:{"code":"000000","msg":"success"}
     * @param bo
     * @return
     */
    @PostMapping(value = "/delete")
    public R delete(@RequestBody ItemBO bo) {
    	itemService.delete(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete/{itemId}")
    public R delete(@PathVariable String itemId) {
    	itemService.delete(itemId);
        return R.ok();
    }

    @PostMapping(value = "/detail/{itemId}")
    public R<ItemBO> detail(@PathVariable String itemId) {
    	ItemBO bo = itemService.detail(itemId);
        return R.ok(bo);
    }

    @PostMapping(value = "/detail")
    public R<ItemBO> detail(@RequestBody ItemBO bo) {
    	ItemBO detail = itemService.detail(bo);
        return R.ok(detail);
    }

    @PostMapping(value = "/update")
    public R<ItemBO> update(@RequestBody ItemBO bo) {
    	itemService.update(bo);
        return R.ok();
    }

    @PostMapping(value = "/query")
    public R<Page<ItemBO>> query(@RequestBody ItemBO bo) {
        Page<ItemBO> result = itemService.query(bo, bo.pageable());
        return R.ok(result);
    }

    @PostMapping(value = "/list")
    public R<List<ItemBO>> list(@RequestBody ItemBO bo) {
        List<ItemBO> result = itemService.list(bo);
        return R.ok(result);
    }

}
